﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Text.RegularExpressions;

namespace Roovent.HttpOperator
{
	public interface IAttributeAccessor
	{
		string GetAttribute(string key);
		void SetAttribute(string key, string value);
	}


	public abstract class HtmlLabel : IAttributeAccessor
	{
		private AttributeCollection attributes = new AttributeCollection();

		#region 公共属性

		public AttributeCollection Attributes
		{
			get { return attributes; }
		}

		public virtual string ID
		{
			get
			{
				if (attributes["id"] != null)
					return attributes["id"];
				else
					return string.Empty;
			}
		}

		public virtual string Name
		{
			get
			{
				if (attributes["name"] != null)
					return attributes["name"];
				else
					return string.Empty;
			}
			set { Attributes["name"] = value; }
		}

		public abstract string LabelName
		{
			get;
		}


		#endregion


		#region 公共函数

		public HtmlLabel() { }

		public HtmlLabel(string label) //形如:<label key1=value1 ...>
		{
			attributes.AddAttributes(label);
		}

		public override string ToString()
		{
			StringBuilder sb = new StringBuilder();
			sb.Append("<" + LabelName);
			foreach (string key in attributes.Keys)
			{
				sb.Append(string.Format(" {0}=\"{1}\"", key, attributes[key]));
			}
			sb.Append(">");
			return sb.ToString();
		}

		#endregion


		#region IAttributeAccessor 成员

		public string GetAttribute(string key)
		{
			return attributes[key];
		}

		public void SetAttribute(string key, string value)
		{
			attributes[key] = value;
		}

		#endregion
	}

	public abstract class HtmlLabelContainer : HtmlLabel
	{
		private HtmlLabelCollection labels = new HtmlLabelCollection();

		public HtmlLabelContainer() { }
		public HtmlLabelContainer(string label) : base(label) { }

		public HtmlLabelCollection Labels
		{
			get { return labels; }
		}
	}

	public class HtmlForm : HtmlLabelContainer
	{
		public HtmlForm() { }
		public HtmlForm(string label) : base(label) { }

		public string Action
		{
			get
			{
				if (Attributes["action"] != null)
					return Attributes["action"];
				else
					return string.Empty;
			}
		}

		public string Method
		{
			get
			{
				if (Attributes["method"] != null)
					return Attributes["method"];
				else
					return string.Empty;
			}
		}

		public string Parameters
		{
			get
			{
				StringBuilder parameters = new StringBuilder();
				foreach (HtmlLabel hl in Labels)
				{
					HtmlInput input = hl as HtmlInput;
					if (input != null)
					{
						if (input.Attributes["type"] != null 
							&& input.Attributes["type"] != "submit"
							&& input.Attributes["type"] != "reset"
							&& input.Attributes["type"] != "button")
						{
							parameters.Append(input.NameValue + "&");
						}
					}
				}
				if (parameters.Length > 0)
				{
					parameters.Remove(parameters.Length - 1, 1); //去掉最后一个'&'
				}
				return parameters.ToString();
			}
		}

		public override string LabelName
		{
			get { return "form"; }
		}
	}

	public class HtmlInput : HtmlLabel
	{
		public HtmlInput() { }
		public HtmlInput(string label) : base(label) { }

		public string NameValue
		{
			get { return string.Format("{0}={1}", Name, Value); }
		}

		public string Value
		{
			get { return Attributes["value"]; }
			set { Attributes["value"] = value; }
		}

		public override string LabelName
		{
			get { return "input"; }
		}
	}
}
